package cn.neusoft.service.impl;

import cn.neusoft.domain.Commodity;
import cn.neusoft.mapper.CommodityMapper;
import cn.neusoft.query.BaseQuery;
import cn.neusoft.service.CommodityService;
import cn.neusoft.vo.PageBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class CommodityServiceImpl implements CommodityService {

    public static final Long WAREHOUSE_ID = Long.valueOf(104);

    @Autowired
    CommodityMapper commodityMapper;

    @Override
    @Transactional
    public void add(Commodity commodity){
        commodityMapper.add(commodity);
        Map<String, Object> params = new HashMap<>();
        params.put("commodity_id",commodity.getCommodity_id());
        params.put("warehouse_id",WAREHOUSE_ID);
        params.put("amount",0L);
        params.put("occupied_amount",0L);
        commodityMapper.addCommodityWarehouse(params);

    }
    @Override
    @Transactional
    public List<Commodity> getAll(){
        List<Commodity> all = commodityMapper.getAll();
        return all;
    }


    @Override
    @Transactional
    public void update(Commodity commodity){
        commodityMapper.update(commodity);
    }
    @Override
    @Transactional
    public void delete(String id){
        commodityMapper.delete(id);
    }

    @Override

    public Commodity getById(String id){
        return commodityMapper.getById(id);
    }

    @Override
    public List<Commodity> getByName(String name) {
        return commodityMapper.getByName(name);
    }

    @Override
    public List<Commodity> getCommoditiesByPage(int page, int pageSize) {
        int start = (page - 1) * pageSize;
        return commodityMapper.getCommoditiesByPage(start, pageSize);
    }

    @Override
    public int getTotalCount() {
        return commodityMapper.getTotalCount();
    }

    @Override
    public List<Commodity> getCommoditiesWithWarehouseInfoByName(String name) {
        return commodityMapper.getCommoditiesWithWarehouseInfoByName(name);
    }

    @Override
    public PageBean<Commodity> findByPage(BaseQuery query){ //构建结果对象
        PageBean<Commodity> pageBean = new PageBean<>();
        //开启分页
        PageHelper.startPage(query.getPageNow(),query.getPageSize());
        //分页查询得到当页的集合
        List<Commodity> commodityList = commodityMapper.findByPage(query);
        //创建一个分页的实体PageInfo
        PageInfo<Commodity> pageInfo = new PageInfo<>(commodityList);

        pageBean.setTotalRows(pageInfo.getTotal());
        pageBean.setTotalPages(pageInfo.getPages());
        pageBean.setRowData(pageInfo.getList());

        return pageBean;};


}
